#include <iostream>

#include <string>
#include <vector>
#include <map>

#include <algorithm>

using namespace std;

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        map<string, vector<string>> map;
        
        for (string& s : strs) {
            string key = s;
            sort(key.begin(), key.end());
            map[key].push_back(s);
        }
        
        vector<vector<string>> result;
        for (auto& pair : map) {
            result.push_back(pair.second);
        }
        
        return result;
    }
};

void test01() {
    cout << "test01:" << endl;
    Solution obj;
    vector<string> strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
    auto re = obj.groupAnagrams(strs);
    for (auto list : re) {
        for (auto str : list) {
            cout << str << " ";
        }
        cout << endl;
    }
}

void test02() {
    cout << "test02:" << endl;
    Solution obj;
    vector<string> strs;
    auto re = obj.groupAnagrams(strs);
    for (auto list : re) {
        for (auto str : list) {
            cout << str << " ";
        }
        cout << endl;
    }
}

void test03() {
    cout << "test03:" << endl;
    Solution obj;
    vector<string> strs = {"a"};
    auto re = obj.groupAnagrams(strs);
    for (auto list : re) {
        for (auto str : list) {
            cout << str << " ";
        }
        cout << endl;
    }
}

int main() {
    test01();
    test02();
    test03();
    return 0;
}
